package com.example.controller;
import com.example.common.Result;
import com.example.entity.*;
import com.example.service.UserService;
import com.example.until.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserService userService;
    @Autowired
    private AuthenticationManager authenticationManager;

    //用户注册
    @PostMapping("/register")
    public Result register(@RequestBody User register){
        return userService.register(register);
    }

    //用户登录
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        if(user.getUsername() == null ){
            return Result.error("用户名不能为空");
        }else if(user.getPassword() == null){
            return Result.error("密码不能为空");
        }
        //认证设置，在后续的方法中，已经设置了连接数据库认证loadUserByUsername
        //先设置认证authentication  这一步Authenticated=false
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword());
        //自动调用loadUserByUsername验证用户名和密码，从数据库中对比查找，如果找到了会返回一个带有认证的封装后的用户，否则会报错，自动处理。（这里我们假设我们配置的security是基于数据库查找的）
        try{
            Authentication authenticate = authenticationManager.authenticate(authenticationToken);
            SecurityContextHolder.getContext().setAuthentication(authenticate);
            User result = userService.login(user);
            String token = JwtUtil.genAccessToken(user.getUsername(),"admin","123");
            result.setToken(token);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error();
        }
    }

    //获取用户详细信息
    @PostMapping("/get/user")
    public Result getUser(@RequestBody User user){
        System.out.println("!!111");
        return userService.getUser(user);
    }

    //更新用户基本信息
    @PostMapping("/update/user")
    public Result updateUser(@RequestBody User user){
        return userService.updateUser(user);
    }

    //增加用户
    @PostMapping("/add/user")
    public Result addUser(@RequestBody User user){
        return userService.addUser(user);
    }

    //增加用户
    @PostMapping("/delete/user")
    public Result deleteUser(@RequestBody User user){
        return userService.deleteUser(user);
    }

    //返回用户
    @PostMapping("/list/user")
    public Result listUser(@RequestBody User user){
        return userService.listUser(user);
    }

    //获取设备概览
    @PostMapping("/get")
    public Result equipment(){
        return userService.equipment();
    }
}
